using System;
using System.Data;
using System.Windows.Forms;
using TUnit.Data;
using TUnit.Class;
namespace viclip
{
    ///<summary>
    ///Date init:  
    ///Date modify:
    ///By:
    ///
    ///</summary>
    public class CQuestion : Class
    {
        #region Declares
        int _questionsId = -1;
        string _contents = "";
        string _answerA = "";
        string _answerB = "";
        string _answerC = "";
        string _answerD = "";
        string _answerCorrect = "";
        int _questionsTypeId = -1;
        int _difficulty = -1;
        int _questionsStatusId = -1;
        #endregion

        #region Constructors
        public CQuestion()
        {
        }
        public CQuestion(CLogin Login)
        {
            base.Login = Login;
        }
        public CQuestion(CLogin Login, object ObjId)
        {
            base.Login = Login;
            using (AccDBQuestion db = new AccDBQuestion())
            {
                this.Populate((CQuestion)db.GetClass(Login, ObjId));
            }
        }
        public CQuestion(CLogin Login, IProvider p, System.Data.DataRow dr)
        {
            base.Login = Login;
            this.Populate(p, dr);
        }
        public CQuestion(CLogin Login, IProvider p, object ObjId)
        {
            base.Login = Login;
            using (AccDBQuestion db = new AccDBQuestion())
            {
                this.Populate((CQuestion)db.GetClass(Login, p, ObjId));
            }
        }
        #endregion

        #region Methods
        public void Populate(IProvider p, DataRow dr)
        {
            if (dr != null)
            {
                try
                {
                    if (dr.Table.Columns.IndexOf("questionsId") >= 0)
                    {
                        this._questionsId = (dr["questionsId"] != DBNull.Value ? Convert.ToInt32(dr["questionsId"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblQuestions ADD questionsId [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("contents") >= 0)
                    {
                        this._contents = (dr["contents"] != DBNull.Value ? Convert.ToString(dr["contents"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblQuestions ADD contents [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("answerA") >= 0)
                    {
                        this._answerA = (dr["answerA"] != DBNull.Value ? Convert.ToString(dr["answerA"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblQuestions ADD answerA [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("answerB") >= 0)
                    {
                        this._answerB = (dr["answerB"] != DBNull.Value ? Convert.ToString(dr["answerB"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblQuestions ADD answerB [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("answerC") >= 0)
                    {
                        this._answerC = (dr["answerC"] != DBNull.Value ? Convert.ToString(dr["answerC"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblQuestions ADD answerC [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("answerD") >= 0)
                    {
                        this._answerD = (dr["answerD"] != DBNull.Value ? Convert.ToString(dr["answerD"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblQuestions ADD answerD [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("answerCorrect") >= 0)
                    {
                        this._answerCorrect = (dr["answerCorrect"] != DBNull.Value ? Convert.ToString(dr["answerCorrect"]) : "");
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblQuestions ADD answerCorrect [nvarchar] (50) NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("questionsTypeId") >= 0)
                    {
                        this._questionsTypeId = (dr["questionsTypeId"] != DBNull.Value ? Convert.ToInt32(dr["questionsTypeId"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblQuestions ADD questionsTypeId [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("difficulty") >= 0)
                    {
                        this._difficulty = (dr["difficulty"] != DBNull.Value ? Convert.ToInt32(dr["difficulty"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblQuestions ADD difficulty [int] NULL");
                    //}
                    if (dr.Table.Columns.IndexOf("questionsStatusId") >= 0)
                    {
                        this._questionsStatusId = (dr["questionsStatusId"] != DBNull.Value ? Convert.ToInt32(dr["questionsStatusId"]) : -1);
                    }
                    //else
                    //{//Tao Field neu bang CSDL chua ton tai Field nay
                    //	p.ExecuteNonQuery("ALTER TABLE tblQuestions ADD questionsStatusId [int] NULL");
                    //}
                }
                catch { }
            }
        }
        public void Populate(CQuestion Obj)
        {
            if (Obj != null)
            {
                try
                {
                    this._questionsId = Obj.questionsId;
                    this._contents = Obj.contents;
                    this._answerA = Obj.answerA;
                    this._answerB = Obj.answerB;
                    this._answerC = Obj.answerC;
                    this._answerD = Obj.answerD;
                    this._answerCorrect = Obj.answerCorrect;
                    this._questionsTypeId = Obj.questionsTypeId;
                    this._difficulty = Obj.difficulty;
                    this._questionsStatusId = Obj.questionsStatusId;
                }
                catch { }
            }
        }
        public override bool Save()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBQuestion db = new AccDBQuestion())
                    {
                        if (this.EventSaving != null)
                            this.EventSaving(this);
                        if (db.Save(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaved != null)
                                this.EventSaved(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventSaveError != null)
                                this.EventSaveError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Save(IProvider p)
        {
            using (AccDBQuestion db = new AccDBQuestion())
            {
                if (db.Save(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        public override bool Delete()
        {
            using (IProvider p = new OleDb(Login.Connectionstring))
            {
                if (p.Open())
                {
                    p.OpenTransaction();
                    using (AccDBQuestion db = new AccDBQuestion())
                    {
                        if (this.EventDeleting != null)
                            this.EventDeleting(this);
                        if (db.Delete(p, this))
                        {
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleted != null)
                                this.EventDeleted(this);
                            return true;
                        }
                        else
                        {
                            p.Rollback();
                            p.CloseTransaction();
                            p.Close();
                            if (this.EventDeleteError != null)
                                this.EventDeleteError(this);
                            return false;
                        }
                    }
                }
            }
            return false;
        }
        public override bool Delete(IProvider p)
        {
            using (AccDBQuestion db = new AccDBQuestion())
            {
                if (db.Delete(p, this))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        #endregion

        #region Events
        public delegate void OnEventSaveError(CQuestion obj);
        public event OnEventSaveError EventSaveError;
        public delegate void OnEventSaving(CQuestion Question);
        public event OnEventSaving EventSaving;
        public delegate void OnEventSaved(CQuestion Question);
        public event OnEventSaved EventSaved;
        public delegate void OnEventDeleting(CQuestion Question);
        public event OnEventSaving EventDeleting;
        public delegate void OnEventDeleted(CQuestion Question);
        public event OnEventDeleted EventDeleted;
        public delegate void OnEventDeleteError(CQuestion obj);
        public event OnEventDeleteError EventDeleteError;
        #endregion

        #region Properties
        public int questionsId
        {
            get { return this._questionsId; }
            set
            {
                if (this._questionsId != value)
                {
                    this._questionsId = value;
                }
            }
        }
        public string contents
        {
            get { return this._contents; }
            set
            {
                if (this._contents != value)
                {
                    this._contents = value;
                }
            }
        }
        public string answerA
        {
            get { return this._answerA; }
            set
            {
                if (this._answerA != value)
                {
                    this._answerA = value;
                }
            }
        }
        public string answerB
        {
            get { return this._answerB; }
            set
            {
                if (this._answerB != value)
                {
                    this._answerB = value;
                }
            }
        }
        public string answerC
        {
            get { return this._answerC; }
            set
            {
                if (this._answerC != value)
                {
                    this._answerC = value;
                }
            }
        }
        public string answerD
        {
            get { return this._answerD; }
            set
            {
                if (this._answerD != value)
                {
                    this._answerD = value;
                }
            }
        }
        public string answerCorrect
        {
            get { return this._answerCorrect; }
            set
            {
                if (this._answerCorrect != value)
                {
                    this._answerCorrect = value;
                }
            }
        }
        public int questionsTypeId
        {
            get { return this._questionsTypeId; }
            set
            {
                if (this._questionsTypeId != value)
                {
                    this._questionsTypeId = value;
                }
            }
        }
        public int difficulty
        {
            get { return this._difficulty; }
            set
            {
                if (this._difficulty != value)
                {
                    this._difficulty = value;
                }
            }
        }
        public int questionsStatusId
        {
            get { return this._questionsStatusId; }
            set
            {
                if (this._questionsStatusId != value)
                {
                    this._questionsStatusId = value;
                }
            }
        }

        CCollection _members = null;
        public CCollection Members
        {
            get
            {
                if (_members == null)
                {
                    using (AccDBAccount db = new AccDBAccount())
                    {
                        CCollection tmp = new CCollection();
                        db.GetCollection(this, tmp);
                        this._members = tmp;
                    }
                }
                return this._members;
            }
        }
        #endregion
    }

    #region Class AccDB
    public class AccDBQuestion : AccDB
    {
        /// <summary>
        /// 
        /// </summary>
        public AccDBQuestion()
        {
        }
        /// <summary>
        ///Phuong thuc dung dien du lieu len Combo 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="cmb"></param>
        /// <param name="extName"></param>
        public override void FillComboBox(CLogin Login, ComboBox cmb, string extName)
        {
            IProvider p = new OleDb(Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT QuestionId, QuestionName";
                sql += " FROM  tblQuestions";
                sql += " WHERE (QuestionId >= 0)";
                sql += " ORDER BY QuestionName";
                DataTable table = p.OpenTable(sql, "tblQuestions");
                if (table != null)
                {
                    if (extName != "")
                    {
                        System.Data.DataRow row = table.NewRow();
                        row["QuestionName"] = extName;
                        row["QuestionId"] = "-1";
                        table.Rows.Add(row);
                    }
                    cmb.DataSource = table;
                    cmb.DisplayMember = "QuestionName";
                    cmb.ValueMember = "QuestionId";
                    p.Close();
                }
            }
        }
        /// <summary>
        ///Doc doi tuong tu CSDL theo ID chi dinh
        /// </summary>
        /// <param name="p"></param>
        /// <param name="ObjId"></param>
        /// <returns></returns>
        public override Class GetClass(CLogin Login, IProvider p, object ObjId)
        {
            try
            {
                string sql = "SELECT * FROM tblQuestions WHERE (QuestionId = " + ObjId + ")";
                DataTable table = p.OpenTable(sql, "tblQuestions");
                if ((table != null) && (table.Rows.Count > 0))
                {
                    return new CQuestion(Login, p, table.Rows[0]);
                }
            }
            catch
            {
            }
            return null;
        }
        /// <summary>
        /// Luu doi tuong vao CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Save(IProvider p, Class obj)
        {
            CQuestion Question = (CQuestion)obj;
            string sql = "SELECT * FROM tblQuestions";
            sql += " WHERE QuestionId =" + Question.questionsId + "";
            //sql +=" AND CompanyId =" + Question.CompanyId + "";
            DataTable dt = p.OpenTable(sql, "tblQuestions");
            if (dt != null)
            {
                DataRow row = null;
                if (dt.Rows.Count == 1 && Question.questionsId != -1)
                {
                    row = dt.Rows[0];
                }
                else
                {
                    Question.questionsId = Convert.ToInt32(p.InitIDKey("QuestionId", "tblQuestions", null));
                    row = dt.NewRow();
                    dt.Rows.Add(row);
                }
                row["questionsId"] = Question.questionsId;
                row["contents"] = Question.contents;
                row["answerA"] = Question.answerA;
                row["answerB"] = Question.answerB;
                row["answerC"] = Question.answerC;
                row["answerD"] = Question.answerD;
                row["answerCorrect"] = Question.answerCorrect;
                row["questionsTypeId"] = Question.questionsTypeId;
                row["difficulty"] = Question.difficulty;
                row["questionsStatusId"] = Question.questionsStatusId;
                if (!p.UpdateTable("tblQuestions", dt))
                {
                    return false;
                }
                return true;
            }
            return false;
        }
        /// <summary>
        ///Xoa doi tuong khoi CSDL 
        /// </summary>
        /// <param name="p"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Delete(IProvider p, Class obj)
        {
            CQuestion Question = (CQuestion)obj;
            string sql = "DELETE FROM tblQuestions";
            sql += " WHERE QuestionId =" + Question.questionsId + "";
            //sql +=" AND CompanyId =" + Question.CompanyId + "";
            return p.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong tren CSDL
        /// </summary>
        /// <param name="p"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(CLogin Login, IProvider p, CCollection Collection)
        {
            string sql = "SELECT * FROM tblQuestions ORDER BY QuestionName";
            DataTable table = p.OpenTable(sql, "tblQuestions");
            if (table != null)
            {
                foreach (DataRow row in table.Rows)
                {
                    CQuestion o = new CQuestion(Login, p, row);
                    Collection.Add(o);
                }
            }
        }
        /// <summary>
        /// Lay ve danh sach cac doi tuong con cua doi obj neu co (danh cho loai du lieu quan he 2 bang cha con)
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        public override void GetCollection(IProvider p, Class obj, CCollection Collection)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        public void GetCollectionStarts(Class obj, CCollection Collection)
        {
             IProvider p = new OleDb(obj.Login.Connectionstring);
             if (p.Open())
             {
                 string sql = "SELECT tblQuestions.*";
                 sql += " FROM tblQuestions INNER JOIN";
                 sql += " tblPackagesAndQuestions ON tblQuestions.questionsId = tblPackagesAndQuestions.questionsId";
                 sql += " WHERE (tblPackagesAndQuestions.packageId = {0} AND (tblPackagesAndQuestions.QuestionsStyle=0))";

                 sql = String.Format(sql, ((CPackages)obj).packageId);

                 DataTable table = p.OpenTable(sql, "tblQuestions");
                 if (table != null)
                 {
                     foreach (DataRow row in table.Rows)
                     {
                         CQuestion o = new CQuestion(obj.Login, p, row);
                         Collection.Add(o);
                     }
                 }
             }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        public void GetCollectionSpeed(Class obj, CCollection Collection)
        {
            IProvider p = new OleDb(obj.Login.Connectionstring);
            if (p.Open())
            {
                string sql = "SELECT tblQuestions.*";
                sql += " FROM tblQuestions INNER JOIN";
                sql += " tblPackagesAndQuestions ON tblQuestions.questionsId = tblPackagesAndQuestions.questionsId";
                sql += " WHERE (tblPackagesAndQuestions.packageId = {0} AND (tblPackagesAndQuestions.QuestionsStyle=2))";

                sql = String.Format(sql, ((CPackages)obj).packageId);

                DataTable table = p.OpenTable(sql, "tblQuestions");
                if (table != null)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        CQuestion o = new CQuestion(obj.Login, p, row);
                        Collection.Add(o);
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Login"></param>
        /// <param name="Collection"></param>
        public void GetCollectionEnd(Class obj, CCollection Collection)
        {

        }
    }
    #endregion
}
